#include "loginwidge.h"
#include "ui_loginwidge.h"
#include "widget.h"
#include "QMessageBox"
LoginWidge::LoginWidge(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LoginWidge)
{
    ui->setupUi(this);
    //程序运行就要开始连接数据库
    ConnectDatabase();
}

LoginWidge::~LoginWidge()
{
    delete ui;
}
void LoginWidge::ConnectDatabase()
{
    QSqlDatabase sql=QSqlDatabase::addDatabase("QMYSQL");
    sql.setDatabaseName("qtdemo1");
    sql.setUserName("root");
    sql.setPassword("123456");
    if(sql.open())qDebug()<<"DataBase start success";
    else qDebug()<<"DataBase start fail";
}
void LoginWidge::on_SignButton_clicked()
{
    QString userName=ui->UserName->text();
    QString passWord=ui->PassWord->text();
    QSqlQuery request;
    request.prepare("select password from user where username=?");
    request.addBindValue(userName);
    request.exec();
    if(request.next() &&request.value(0).toString()==passWord)
    {
        this->close();
        //打开主页面对象并且把用户名传过去，因为需要查询上次登录的时间
        Widget *w=new Widget(userName);
        w->show();
    }
    else
    {
        QMessageBox::warning(NULL,"error","error!");
    }
}

void LoginWidge::on_ResetButton_clicked()
{
    ui->UserName->setText("");
    ui->PassWord->setText("");
}
